import actions from 'actions/operation'
import { saveData } from 'utils'
import { message } from 'antd'

export default {
  state: {
  },
  reducers: {
    save (state, payload) {
      return saveData(state, payload)
    }
  },
  effects: dispatch => ({
    // 获取banner列表
    async getBannerList (params) {
      this.save({
        bannerData: null
      })
      const res = await actions.getBannerList(params)
      if (res.success) {
        this.save({
          bannerData: {
            list: res.data.bannerList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 创建banner
    async createBanner (params) {
      this.save({
        createRequest: false
      })
      const res = await actions.createBanner(params)
      if (res.success) {
        this.save({
          createRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除banner
    async delBanner (params) {
      this.save({
        delBannerRequest: false
      })
      const res = await actions.delBanner(params)
      if (res.success) {
        this.save({
          delBannerRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 更改banner状态
    async changeBannerStatus (params) {
      this.save({
        statusRequest: false
      })
      const res = await actions.changeBannerStatus(params)
      if (res.success) {
        this.save({
          statusRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 编辑banner
    async editBanner (params) {
      this.save({
        editBannerRequest: false
      })
      const res = await actions.createBanner(params)
      if (res.success) {
        this.save({
          editBannerRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取活动列表
    async getActivityList (params) {
      this.save({
        activityData: null
      })
      const res = await actions.getActivityList(params)
      if (res.success) {
        this.save({
          activityData: {
            list: res.data.activityList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 创建活动
    async createActivity (params) {
      this.save({
        createActivityRequest: false
      })
      const res = await actions.createActivity(params)
      if (res.success) {
        this.save({
          createActivityRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 编辑活动
    async editActivity (params) {
      this.save({
        editActivityRequest: false
      })
      const res = await actions.createActivity(params)
      if (res.success) {
        this.save({
          editActivityRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除活动
    async delActivity (params) {
      this.save({
        delActivityRequest: false
      })
      const res = await actions.delActivity(params)
      if (res.success) {
        this.save({
          delActivityRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 更新活动状态
    async changeActivityStatus (params) {
      this.save({
        activityStatusRequest: false
      })
      const res = await actions.changeActivityStatus(params)
      if (res.success) {
        this.save({
          activityStatusRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取视频列表
    async getRadioList (params) {
      this.save({
        radioData: null
      })
      const res = await actions.getRadioList(params)
      if (res.success) {
        this.save({
          radioData: {
            list: res.data.videoList,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 创建视频
    async createRadio (params) {
      this.save({
        createRadioRequest: false
      })
      const res = await actions.createRadio(params)
      if (res.success) {
        this.save({
          createRadioRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 编辑视频
    async editRadio (params) {
      this.save({
        editRadioRequest: false
      })
      const res = await actions.createRadio(params)
      if (res.success) {
        this.save({
          editRadioRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除视频
    async delRadio (params) {
      this.save({
        delRadioRequest: false
      })
      const res = await actions.delRadio(params)
      if (res.success) {
        this.save({
          delRadioRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 更新视频状态
    async changeRadioStatus (params) {
      this.save({
        radioStatusRequest: false
      })
      const res = await actions.changeRadioStatus(params)
      if (res.success) {
        this.save({
          radioStatusRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取推送列表
    async getPushList (params) {
      this.save({
        pushData: null
      })
      const res = await actions.getPushList(params)
      if (res.success) {
        this.save({
          pushData: {
            total: res.data.totalCount,
            list: res.data.templateDetailList
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 新建推送
    async createPush (params) {
      this.save({
        createPushRequest: false
      })
      const res = await actions.operatePush(params)
      if (res.success) {
        this.save({
          createPushRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 编辑推送
    async editPush (params) {
      this.save({
        editPushRequest: false
      })
      const res = await actions.operatePush(params)
      if (res.success) {
        this.save({
          editPushRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 发布模板
    async publishTemplate (params) {
      this.save({
        publishRequest: false
      })
      const res = await actions.publishTemplate(params)
      if (res.success) {
        this.save({
          publishRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 删除模板
    async delTemplte (params) {
      this.save({
        delRequest: false
      })
      const res = await actions.delTemplte(params)
      if (res.success) {
        this.save({
          delRequest: true
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取被推送者
    async getPushed (params) {
      this.save({
        pushedData: null
      })
      const res = await actions.getPushed(params)
      if (res.success) {
        this.save({
          pushedData: {
            list: res.data.users,
            total: res.data.totalCount
          }
        })
      } else {
        message.error(res.msg)
      }
    },
    // 获取推送详情
    async getPushDetail (params) {
      this.save({
        pushDetail: null
      })
      const res = await actions.getPushDetail(params)
      if (res.success) {
        this.save({
          pushDetail: res.data
        })
      } else {
        message.error(res.msg)
      }
    }
  })
}
